home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_10 / phillips.exe / HIST.C < prev    next >
Text File  |  1990-09-08  |  12KB  |  499 lines

  1.  
  2.  
  3.  
  4.        /*****************************************************
  5.        *
  6.        *       file d:\cips\hist.c
  7.        *
  8.        *       Functions: This file contains
  9.        *           calculate_histogram
  10.        *           calculate_histogram
  11.        *           zero_histogram
  12.        *           perform_histogram_equalization
  13.        *           show_histogram
  14.        *           print_histogram
  15.        *           smooth_histogram
  16.        *
  17.        *
  18.        *       Purpose: These functions calculate and display the
  19.        *          histogram of an input image array.
  20.        *
  21.        *       Modifications:
  22.        *           July 86 - ported to IBM-PC
  23.        *           August 1990 - modified for use in the
  24.        *               C Image Processing System
  25.        *
  26.        *******************************************************/
  27.  
  28. #include "d:\cips\cips.h"
  29.  
  30.  
  31. #define PRINT_WIDTH  80
  32. #define FORMFEED     '\014'
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.         /*****************************************
  43.         *
  44.         *    perform_histogram_equalization(...
  45.         *
  46.         ******************************************/
  47.  
  48. perform_histogram_equalization(image, histogram, new_grays, area)
  49.    float new_grays, area;
  50.    short image[ROWS][COLS];
  51.    unsigned long histogram[];
  52. {
  53.    int i,
  54.        j,
  55.        k;
  56.    unsigned long sum,
  57.             sum_of_h[256];
  58.  
  59.    double constant;
  60.  
  61.  
  62.  
  63.    sum = 0;
  64.    for(i=0; i<256; i++){
  65.       sum         = sum + histogram[i];
  66.       sum_of_h[i] = sum;
  67.    }
  68.  
  69.       /* constant = new # of gray levels div by area */
  70.    constant = new_grays/area;
  71.    for(i=0; i<ROWS; i++){
  72.       for(j=0; j<COLS; j++){
  73.          k           = image[i][j];
  74.          image[i][j] = sum_of_h[k] * constant;
  75.       }
  76.    }
  77. }  /* ends perform_histogram_equalization */
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.         /*****************************************
  90.         *
  91.         *   zero_histogram(...
  92.         *
  93.         ******************************************/
  94.  
  95. zero_histogram(histogram)
  96.    unsigned long histogram[];
  97. {
  98.    int i;
  99.    for(i=0; i<256; i++)
  100.       histogram[i] = 0;
  101. }  /* ends zero_histogram */
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.         /*****************************************
  111.         *
  112.         *   calcualte_histogram(...
  113.         *
  114.         ******************************************/
  115.  
  116. calculate_histogram(image, histogram)
  117.    short  image[ROWS][COLS];
  118.    unsigned long histogram[];
  119. {
  120.    int i,j,k;
  121.    for(i=0; i<ROWS; i++){
  122.       for(j=0; j<COLS; j++){
  123.          k = image[i][j];
  124.          histogram[k] = histogram[k] + 1;
  125.       }
  126.    }
  127. }  /* ends calculate_histogram */
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.         /*********************************************
  137.         *
  138.         *       show_histogram(histogram)
  139.         *
  140.         *        This function shows the histogram
  141.         *        on the screen as numbers and stars.
  142.         *
  143.         **********************************************/
  144.  
  145.  
  146.  
  147. show_histogram(histogram)
  148.         unsigned long histogram[];
  149. {
  150.         int     count,
  151.                 i,
  152.                 j;
  153.         unsigned long max, scale;
  154.  
  155.  
  156.         max   = 0;
  157.         count = 0;
  158.  
  159.         for(i=0; i<GRAY_LEVELS; i++)
  160.            if(histogram[i] > max)
  161.               max = histogram[i];
  162.  
  163.         if(max > (70 - 12))
  164.            scale = max/(70 - 12);
  165.         else
  166.            scale = 1;
  167.  
  168.         printf("\n max=%ld scale=%ld",max, scale);
  169.  
  170.         printf("\n\ngray    count");
  171.         printf("\nlevel");
  172.  
  173.         for(i=0; i<256; i++){
  174.            if(histogram[i] == 0)
  175.               ++count;
  176.            else 
  177.               count = 0;
  178.  
  179.            if(count < 2){
  180.               printf("\n %4d: %7ld",i,histogram[i]);
  181.               for(j=0; j<((int)(histogram[i]/scale)); j++){
  182.                  printf("*");
  183.               }         /* ends loop over j             */
  184.            }            /* ends if count < 5            */
  185.         }               /* ends loop over i GRAY_LEVELS */
  186. }       /* ends show_histogram */
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.         /*********************************************
  197.         *
  198.         *        print_histogram(histogram)
  199.         *
  200.         *        This function prints the histogram
  201.         *       input to the function.
  202.         *
  203.         **********************************************/
  204.  
  205.  
  206.  
  207. print_histogram(histogram, name)
  208.         char name[];
  209.         unsigned long histogram[];
  210. {
  211.         char    string[300],
  212.                 output[300];
  213.  
  214.         int     count,
  215.                 i,
  216.                 j,
  217.                 line_counter,
  218.                 print_counter;
  219.         unsigned long scale, max;
  220.  
  221.         FILE *printer;
  222.  
  223.         if( (printer = fopen("prn", "w")) == NULL)
  224.            printf("\nPH> Could not open printer");
  225.         else
  226.            printf("\nPH> The print file is opened");
  227.  
  228.         max           = 0;
  229.         count         = 0;
  230.         print_counter = 0;
  231.  
  232.         for(i=0; i<256; i++)
  233.            if(histogram[i] > max)
  234.               max = histogram[i];
  235.  
  236.         if(max > (PRINT_WIDTH - 12))
  237.            scale = max/(PRINT_WIDTH - 12);
  238.         else
  239.            scale = 1;
  240.  
  241.         printf("\n max=%ld scale=%ld",max, scale);
  242.  
  243.         printf("\nPI> Print header");
  244.         line_counter = 0;
  245.         long_clear_buffer(string);
  246.         sprintf(string, "          This image is -- %s --", name);
  247.         my_fwriteln(printer, string);
  248.         ++line_counter;
  249.  
  250.         long_clear_buffer(string);
  251.         sprintf(string, " ");
  252.         my_fwriteln(printer, string);
  253.         ++line_counter;
  254.  
  255.         long_clear_buffer(string);
  256.         sprintf(string, "          gray    count");
  257.         my_fwriteln(printer, string);
  258.         ++line_counter;
  259.         long_clear_buffer(string);
  260.         sprintf(string, "          level");
  261.         my_fwriteln(printer, string);
  262.         ++line_counter;
  263.  
  264.         for(i=0; i<256; i++){
  265.            if(histogram[i] == 0)
  266.               ++count;
  267.            else 
  268.               count = 0;
  269.  
  270.            if(count < 2){
  271.               printf(" %4d: %7ld",i,histogram[i]);
  272.               print_counter++;
  273.               if(print_counter >= 6){
  274.                  printf("\n");
  275.                  print_counter = 0;
  276.               }  /* ends if print_counter >= 6 */
  277.  
  278.               long_clear_buffer(string);
  279.               sprintf(string, 
  280.                 "           %3d: %7ld ->",i,histogram[i]);
  281.               my_fwrite(printer, string);
  282.               long_clear_buffer(string);
  283.               sprintf(output, " ");
  284.               for(j=0; j<((int)(histogram[i]/scale)); j++){
  285.                  sprintf(string, "*");
  286.                  append_string(string, output);
  287.               }         /* ends loop over j                */
  288.               my_fwriteln(printer, output);
  289.               ++line_counter;
  290.               if(line_counter >= 55){
  291.                  line_counter = 0;
  292.                  putc(FORMFEED, printer);
  293.               }  /* ends if line_counter >=55  */
  294.  
  295.            }                    /* ends if count < 2 */
  296.         }                 /* ends loop over i */
  297.         putc(FORMFEED, printer);
  298.         fclose(printer);
  299.  
  300. }        /* ends print_histogram */
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.         /*********************************************
  310.         *
  311.         *       display_histogram(histogram)
  312.         *
  313.         *       This function shows the histogram
  314.         *       input to the function.
  315.         *
  316.         **********************************************/
  317.  
  318.  
  319.  
  320. display_histogram(histogram, x, y,
  321.                   line_color,data_color)
  322.         int data_color, line_color, x, y;
  323.         unsigned long histogram[];
  324. {
  325.         int     count,
  326.                 i,
  327.                 j,
  328.                 length;
  329.         unsigned long scale, max;
  330.  
  331.         max   = 0;
  332.         count = 0;
  333.  
  334.         for(i=0; i<256; i++)
  335.            if(histogram[i] > max)
  336.               max = histogram[i];
  337.  
  338.         if(max > (300 - 12))
  339.            scale = max/(100 - 12);
  340.         else
  341.            scale = 1;
  342.  
  343.  
  344.    /***************************
  345.    *
  346.    *   clear out an area for
  347.    *   this histogram display
  348.    *
  349.    ****************************/
  350.  
  351.